// pages/shop/shop.js
const app = getApp()
var WxParse = require('../../wxParse/wxParse.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
      shop:{},
      ShopPopup:true,
      buyNumber:1,
      price:0,
      sumPrice:0,
      canSubmit:false,
      reputation:null,
      numCar:0
  },
  closePopupTap:function(){
    this.setData({
      ShopPopup: true
    })
  },
  showPop:function(){
      this.setData({
        ShopPopup:false
      })
  },
  // 数量添加
  addNum:function(){
    var buyNumber=this.data.buyNumber+1;
    if (buyNumber >= this.data.shop.basicInfo.stores){
      buyNumber = this.data.shop.basicInfo.stores;
    }
    console.log(buyNumber)
    this.setData({
      buyNumber: buyNumber,
      sumPrice: this.data.price * buyNumber
    })

  },
  reduceNum:function(){
    var buyNumber = this.data.buyNumber;
    if (buyNumber>1){
      buyNumber -= 1;
    }
   
    this.setData({
      buyNumber: buyNumber,
      sumPrice: this.data.price * buyNumber
    })
  },
  //初始化规格数据 给所有规格加一个参数 check
  InitProperties:function(){
      var shop=this.data.shop;
      if(shop.properties){
        for (var i = 0; i < shop.properties.length; i++) {
          for (var j = 0; j < shop.properties[i].childsCurGoods.length; j++) {
            shop.properties[i].childsCurGoods[j].check = false;
          }

        }
        this.setData({
          shop: shop
        })
      }
    
      

  },
  // 点击规格
  ItemTap:function(e){
    //获取点击的规格是第几个
    var index = e.currentTarget.dataset.index;
    //获取主规格第几个
    var idx = e.currentTarget.dataset.idx;
    //获取选中规格的ID
   

    var shop=this.data.shop;
   
    for (var j = 0; j < shop.properties[idx].childsCurGoods.length; j++) {
      shop.properties[idx].childsCurGoods[j].check = false;
       }

    shop.properties[idx].childsCurGoods[index].check = true;
    this.setData({
      shop: shop
    })

    //根据规格后去价格
    this.getPrice()
  },
  //根据规格获取价格
  getPrice:function(){
    //
    var shop=this.data.shop;
    var itemChecked=[]

    //遍历数据，获取那些数据是被选中的
    for (var i = 0; i < shop.properties.length; i++) {
      for (var j = 0; j < shop.properties[i].childsCurGoods.length; j++) {
        if (shop.properties[i].childsCurGoods[j].check==true){
          //将选中的规格放到数组中
          itemChecked.push(shop.properties[i].childsCurGoods[j])
        }
      }
    }
    //如果所有规格都被选中
    if (itemChecked.length >= shop.properties.length){
        
      //根据选中的规格请求最新的价格
      //先创建参数
      var str = "";
      for (var i = 0; i < itemChecked.length; i++) {
        str += itemChecked[i].propertyId + ":" + itemChecked[i].id + ","
      }
      var data = {
        goodsId: shop.basicInfo.id,
        propertyChildIds: str
      }
      //根据规格请求数据
      wx.request({
        url: app.globalData.BaseUrl + '/shop/goods/price',
        data: data,
        success: (res => {
          console.log(res.data)
          this.setData({
            //请求的价格赋值到新的数据中 计算最新价格
            price:res.data.data.price,
              sumPrice:res.data.data.price * this.data.buyNumber,
              //可提交状态变成true
            canSubmit:true
          })
        })
      })
    }

    
  },
  // 跳转到购物车
  goShopCar(){
    wx.switchTab({
      url: '/pages/cart/cart'
    })
  },

  // 加入购物车
  addShopCar: function () {
    //如果商品有规格切规格没有全部选中
    //判断所有规格都选中 
    console.log(this.data.shop.properties)
    if (this.data.shop.properties&&!this.data.canSubmit) {
      if (!this.data.canSubmit) {
        wx.showModal({
          title: '提示',
          content: '请选择商品规格！',
          showCancel: false
        })
      }
     
      return;
    }
    //购买数量不能小于1
    if (this.data.buyNumber < 1) {
      wx.showModal({
        title: '提示',
        content: '购买数量不能为0！',
        showCancel: false
      })
      return;
    }
   // 提交到购物车需要这个商品的基本信息 ，选中的规格，数量
    var itemChecked = []
    var shop=this.data.shop;
    if(shop.properties){
      for (var i = 0; i < shop.properties.length; i++) {
        for (var j = 0; j < shop.properties[i].childsCurGoods.length; j++) {
          if (shop.properties[i].childsCurGoods[j].check == true) {
            //将选中的规格放到数组中
            itemChecked.push(shop.properties[i].childsCurGoods[j])
          }
        }
      }
    }
    //遍历数据，获取那些数据是被选中的
   
    var shopCar=[
      {
       price: this.data.price ,  
        sumprice: this.data.sumPrice,
         buyNum:this.data.buyNumber,
        shopInfo: this.data.shop.basicInfo,
       itemChecked: itemChecked,
        checked:true
     }
    ]
    

    var that=this;
    wx.getStorage({
      key: 'shopcat',
      success: function(res) {
         
          var newshop=res.data.concat(shopCar)
        wx.setStorage({
          key: 'shopcat',
          data: newshop,
          success: function () {


            wx.showToast({
              title: '加入购物车成功',
              icon: 'success',
              duration: 2000
            })
            
          }
        })
      },
      fail:function(err){

    wx.setStorage({
      key: 'shopcat',
      data: shopCar,
      success: function () {


        wx.showToast({
          title: '加入购物车成功',
          icon: 'success',
          duration: 2000
        })
      
        
      }
    })
      }
    })
    that.setData({
      ShopPopup: true,
      //同步显示购物车种的数量
      numCar:that.data.numCar+that.data.buyNumber
    })
   
  },
  syncCarNum(){
      //获取购物车中物品的数量
      var that=this;
      wx.getStorage({
        key: 'shopcat',
        success: function(res) {
            console.log(res.data)
            var sum=0;
            for(var i=0;i<res.data.length;i++){
              sum+=res.data[i].buyNum;
            }
            that.setData({
              numCar:sum
            })
        },
      })

  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
      console.log(options.id)
    //获取商品
   
    var that = this;
      wx.request({
        url: app.globalData.BaseUrl + '/shop/goods/detail',
        data:{id:options.id},
        success:(res=>{
          console.log(res.data)
          var shopContent = res.data.data.content;
          WxParse.wxParse('shopContent', 'html', shopContent, that, 5);
          this.setData({
            shop:res.data.data,
            price: res.data.data.basicInfo.originalPrice,
            sumPrice:res.data.data.basicInfo.originalPrice
          })
          
          this.InitProperties()
        })
      })
      //获取评论
    wx.request({
      url: app.globalData.BaseUrl  + '/shop/goods/reputation',
      data: {
        goodsId: options.id
      },
      success: function (res) {
        console.log(res.data);
        if (res.data.code == 0) {
          //console.log(res.data.data);
          that.setData({
            reputation: res.data.data
          });
        }
      }
    })
    this.syncCarNum()

  },



  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})